package com.peanut.baby.mvp.qadetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.QA;
import com.peanut.baby.mvp.bind.BindMobileActivity;
import com.peanut.baby.mvp.login.LoginActivity;
import com.peanut.baby.mvp.main.expert.qa.QARecyclerAdapter;
import com.peanut.devlibrary.BaseActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.TitleLayout;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class QAListActivity extends BaseActivity implements TitleLayout.OnTitleClickListener,
        PullRecyclerView.OnRecyclerRefreshListener,
        QAListContract.View, QARecyclerAdapter.OnItemViewClickListener{
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.bbs_list)
    PullRecyclerView recycle;


    private int pageNo  = 1;
    private int pageCount = 5;

    private QARecyclerAdapter adapter;
    private List<QA>  qas;
    private QAListPresenter presenter;


    public static void start(Context ctx){
        ctx.startActivity(new Intent(ctx, QAListActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle);
        ButterKnife.bind(this);

        title.setOnTitleClickedListener(this);
        title.setTitle("问答");
        recycle.setOnRecyclerRefreshListener(this);
        presenter = new QAListPresenter(this,this);
        qas = new ArrayList<>();
        adapter = new QARecyclerAdapter(this,qas);
        adapter.setOnClickListener(this);
        recycle.setLayoutManager(new XLinearLayoutManager(this));
        recycle.setAdapter(adapter);

        recycle.postRefreshing();

    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onPullRefresh() {
        pageNo = 1;
        presenter.getQAList(pageNo,pageCount);
    }

    @Override
    public void onLoadMore() {
        pageNo += 1;
        presenter.getQAList(pageNo,pageCount);
    }

    @Override
    public void onViewItemClicked(View v, QA item) {
        QADetailActivity.start(this,item.id);
    }

    @Override
    public void onViewPraiseClicked(QA qa) {
        if(InitManager.getInstance().getUser() == null){
            LoginActivity.startForResult(this);
            return;
        }
        if(StringUtil.isNullOrEmpty(InitManager.getInstance().getUser().userToken)){
            BindMobileActivity.startForResult(this);
            return;
        }
        presenter.praiseQA(qa);
    }

    private void stopRefreshAndLoad(){
        recycle.stopRefresh();
        recycle.stopLoadMore();
    }

    @Override
    public void onGetQAListSuccess(List<QA> _qas) {
        stopRefreshAndLoad();
        if(_qas != null && !_qas.isEmpty()){
            if(pageNo == 1){
                qas.clear();
            }
            qas.addAll(_qas);
            adapter.notifyDataSetChanged();
            recycle.enableLoadMore(_qas.size() >= pageCount); // 当剩余还有大于0页的数据时，开启上拉加载更多
        }
    }

    @Override
    public void onRequestFailed(String reason) {
        stopRefreshAndLoad();
        showToast(reason);
    }

    @Override
    public void onPraiseSuccess(QA qa) {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onPraiseFailed(String reason) {
        adapter.notifyDataSetChanged();
        showToast(reason);
    }
}
